package com.yikaiye.android.yikaiye.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemMessageLayoutBinding;
import com.yikaiye.android.yikaiye.model.MessageRecordsModel;
import com.yikaiye.android.yikaiye.ui.activity.service.ServiceDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;

public class MessageListAdapter extends BaseQuickAdapter<MessageRecordsModel.MessageModel, DataBindingHolder<ItemMessageLayoutBinding>> {

    private final Context context;
    private OnDeleteItemClickListener onDeleteItemClickListener;

    public MessageListAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemMessageLayoutBinding> itemChooseCompanyBindingDataBindingHolder, int i, @Nullable MessageRecordsModel.MessageModel messageModel) {
        ItemMessageLayoutBinding binding = itemChooseCompanyBindingDataBindingHolder.getBinding();
        if (messageModel != null) {
            binding.tvMsgContent.setText(CommonUtil.checkText(messageModel.getMessage()));
            binding.tvMsgTime.setText(CommonUtil.checkText(messageModel.getCreateTime()));
            binding.tvDot.setVisibility(messageModel.getMessageStatus() == 0 ? View.VISIBLE : View.GONE);
            binding.llDelete.setOnClickListener(view -> onDeleteItemClickListener.onDeleteItemClickListener(messageModel.getId()));
            if (null == messageModel.getType()) {
                binding.tvMsgTitle.setText(context.getString(R.string.system_notifications));
            } else {
                // 消息类型 （付款成功 0；过期提醒 1；退款成功 2；付款取消 3； 服务消息 4）
                switch (messageModel.getType()) {
                    case 0:
                        binding.tvMsgTitle.setText(context.getString(R.string.payment_successful));
                        break;
                    case 1:
                        binding.tvMsgTitle.setText(context.getString(R.string.expiration_reminder));
                        break;
                    case 2:
                        binding.tvMsgTitle.setText(context.getString(R.string.refund_successful));
                        break;
                    case 3:
                        binding.tvMsgTitle.setText(context.getString(R.string.payment_cancel));
                        break;
                    case 4:
                        binding.tvMsgTitle.setText(context.getString(R.string.service_message));
                        break;
                }
            }
            itemChooseCompanyBindingDataBindingHolder.itemView.setOnClickListener(view -> {
                if (messageModel.getType() != null && messageModel.getType() == 4) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.FROM_KEY, Constant.service);
                    bundle.putString(Constant.MESSAGE_ID_KEY, messageModel.getId());
                    JumpIntent.jump((Activity) context, ServiceDetailsActivity.class, bundle);
                }
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemMessageLayoutBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_message_layout, viewGroup);
    }

    public interface OnDeleteItemClickListener {
        void onDeleteItemClickListener(String id);
    }

    public void setOnChildItemClickListener(OnDeleteItemClickListener onDeleteItemClickListener) {
        this.onDeleteItemClickListener = onDeleteItemClickListener;
    }
}
